package susebs.pb.travel_server.controller;

import com.github.pagehelper.PageInfo;
import susebs.pb.travel_server.domain.PageDto;
import susebs.pb.travel_server.domain.Result;
import susebs.pb.travel_server.entity.Pictures;
import susebs.pb.travel_server.service.PicturesService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;

/**
 * (Pictures)表控制层
 *
 * @author makejava
 * @since 2024-02-06 19:38:35
 */
@RestController
@RequestMapping("/pictures")
@CrossOrigin
public class PicturesController {
    /**
     * 服务对象
     */
    @Resource
    private PicturesService picturesService;

    /**
     * 查询用户头像列表（related_entity_id == 4）
     * 无参数
     * @return 用户头像列表
     */
    @GetMapping("/userImgList")
    public Result userImgList(){
        ArrayList<Pictures> list = picturesService.queryPicturesByOwner(4, null);
        return Result.buildSuccess(list);
    }

    /**
     * 获取指定用户的头像
     * @param userId 用户id
     * @return 用户头像
     */
    @GetMapping("/getUserImg")
    public Result getUserImg(Long userId){
        Pictures picture = picturesService.queryPicturesByOwner(4, userId).get(0);
        String res = picture.getPicLink();
        return Result.buildSuccess(res);
    }

    //管理
    //查
    @PostMapping("/queryByPage")
    public Result queryByPage(@RequestBody PageDto pageDto){
        PageInfo res = picturesService.queryAllPicturesByPage(pageDto);
        if(res.getList().isEmpty())return Result.buildFail("无图片数据");
        return Result.buildSuccess(res);
    }
    //删
    @GetMapping("/deleteById")
    public Result deleteById(Long picId){
        boolean delete = picturesService.deleteById(picId);
        if(delete)return Result.buildSuccess();
        return Result.buildFail("删除失败！");
    }


}

